package cn.dm.service;

import cn.dm.client.RestDmItemClient;
import cn.dm.client.RestDmItemCommentClient;
import cn.dm.client.RestDmItemTypeClient;
import cn.dm.common.Dto;
import cn.dm.common.DtoUtil;
import cn.dm.common.EmptyUtils;
import cn.dm.common.RedisUtils;
import cn.dm.pojo.DmItem;
import cn.dm.pojo.DmItemType;
import cn.dm.pojo.DmUser;
import cn.dm.vo.DmFloorItems;
import cn.dm.vo.DmItemTypeVo;

import cn.dm.vo.HotItemVo;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DmitemServiceImpl implements DmitemService {
    @Resource
    private RestDmItemClient restDmItemClient;
    @Resource
    private RestDmItemTypeClient restDmItemTypeClient;
    @Resource
    private RestDmItemCommentClient restDmItemCommentClient;

    @Override
    public Dto<DmItemTypeVo> selectTestChildren() throws Exception {
        List<DmItemTypeVo> dmItemTypeVos = restDmItemTypeClient.selectTestChildren();

        return DtoUtil.returnDataSuccess(dmItemTypeVos);
    }
    @Override
    public Dto queryTransverse() throws Exception {

        List<DmItemTypeVo> dmItemTypeVos = restDmItemTypeClient.selectTestChildren();

        return DtoUtil.returnDataSuccess(dmItemTypeVos);
    }

    @Override
    public Dto queryHotItems(Integer itemTypeId) throws Exception {
        System.out.println(itemTypeId);
        Map param=new HashMap();
        param.put("cinemaId",itemTypeId);

        List hotDmItemList = restDmItemClient.getHotDmItemList(param);

        return DtoUtil.returnDataSuccess(hotDmItemList);
    }

    @Override
    public Dto queryFloorItems() throws Exception {
        List<DmFloorItems> dmFloorItems = restDmItemClient.queryItemByFloor();

        return DtoUtil.returnDataSuccess(dmFloorItems);
    }

    @Override
    public Dto queryTodayRecommend() throws Exception {
        List<DmItemTypeVo> dmItemTypeVos = restDmItemTypeClient.selectTestChildren();
        List<HotItemVo> hotItems = null;
        for (int i=1;i<dmItemTypeVos.size();i++){
            hotItems =  dmItemTypeVos.get(i).getHotItems();
        }

        return DtoUtil.returnDataSuccess(hotItems);
    }

    @Override
    public Dto queryToSaleItem() throws Exception {
        List<DmItemTypeVo> dmItemTypeVos = restDmItemTypeClient.selectTestChildren();
        List<HotItemVo> hotItems = null;
        for (int i=1;i<dmItemTypeVos.size();i++){
            hotItems =  dmItemTypeVos.get(i).getHotItems();
        }

        return DtoUtil.returnDataSuccess(hotItems);
    }

    @Override
    public Dto queryBanner() throws Exception {
        List<DmItemTypeVo> dmItemTypeVos = restDmItemTypeClient.selectTestChildren();
        List<HotItemVo> hotItems = null;
        for (int i=1;i<dmItemTypeVos.size();i++){
            hotItems =  dmItemTypeVos.get(i).getHotItems();
        }

        return DtoUtil.returnDataSuccess(hotItems);
    }

    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private RedisUtils redisUtils;
    @Override
    public Dto loadCurrentUserByToken(String  token) throws Exception {

        Dto dto=null;
        Object o = redisUtils.get(token);
        if (EmptyUtils.isEmpty(o)){
          dto=  DtoUtil.returnFail("用户未登录","1001");
        }else {

             DmUser dmUser = objectMapper.readValue(o.toString(),DmUser.class);
           dto= DtoUtil.returnDataSuccess(0);
        }


        return dto;
    }
}
